﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Infrastructure;
using Enterprise.Service.Jihuabu;
using Enterprise.Model.Jihuabu;

namespace Enterprise.UI.Web.Modules.Jihuabu
{
    public partial class M01_JDBView : System.Web.UI.Page
    {
        string y1 = (string)Utility.sink("y1", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        string y2 = (string)Utility.sink("y2", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);

        string w1 = (string)Utility.sink("w1", Utility.MethodType.Get,0,0, Utility.DataType.Str);
        string w2 = (string)Utility.sink("w2", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(y1))                   
                    t1.Text = y1;

                if (!string.IsNullOrEmpty(y2))
                   
                    t2.Text = y2;

                if(!string.IsNullOrEmpty(w1))
                    tb_wcl1.Text=w1;

                if (!string.IsNullOrEmpty(w2))
                    tb_wcl2.Text = w2;

                bindGrid();
            }
        }

        //绑定表格
        private void bindGrid()
        {
            M01JDBService service = new M01JDBService();

            string str = "";
            if (t1.Text != "")
            {
                str += " and x2.kgsj >='"+t1.Text+"' ";
            }
            if (t2.Text != "")
            {
                str += " and x2.kgsj <='" + t2.Text + "' ";
            }

            if (tb_afebh.Text != "")
            {
                str += " and x2.afebh = '"+tb_afebh.Text+"'";
            }

            if (tb_dw.Text != "")
            {
                str += " and x2.xmssgs ='"+tb_dw.Text+"'";
            }

            if (tb_wcl1.Text != "")
            {
                str += " and x2.wcl >= "+tb_wcl1.Text;
            }
            if (tb_wcl2.Text != "")
            {
                str += " and x2.wcl<="+tb_wcl2.Text;
            }

            string sql = @"select x1.afebh,x2.* from 
(
select afebh,max(s.wcsj) as wcsj from 
(
select  substr(b.kgsj,0,4) as nf,a.afebh,b.xmfl,b.xmmc,b.xmssgs,b.afepz_my,b.afepz_jg,b.shiji_jg,b.zt,a.wcsj,a.wc_jg,b.kgsj,nvl(round(a.wc_jg/b.afepz_jg,4)*100,0) as wcl from jhb_m01_jdb a ,jhb_m01_xmb b where  a.afebh=b.afebh(+)
) s group by afebh
) x1
left join 

(select  substr(b.kgsj,0,4) as nf,a.afebh,b.xmfl,b.xmmc,b.xmssgs,b.afepz_my,b.afepz_jg,b.shiji_jg,b.zt,a.wcsj,a.wc_jg,b.kgsj,nvl(round(a.wc_jg/b.afepz_jg,4)*100,0) as wcl from jhb_m01_jdb a ,jhb_m01_xmb b where  a.afebh=b.afebh(+)
) x2

on x1.afebh=x2.afebh and x1.wcsj = x2.wcsj where x1.afebh is not null ";

            gv.DataSource = service.GetListbySQL(sql+str);
            gv.DataBind();
        }
        
        protected void d_rq_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindGrid();
        }        

        protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gv.PageIndex = e.NewPageIndex;
            bindGrid();
            TextBox tb = (TextBox)gv.BottomPagerRow.FindControl("inPageNum");
            tb.Text = (gv.PageIndex + 1).ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)gv.BottomPagerRow.FindControl("inPageNum");
            int num = Int32.Parse(tb.Text);
            GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
            gv_PageIndexChanging(null, ea);
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            bindGrid();
        }
    }
}